package com.bifrost.model.vo;

import com.bifrost.entity.Permission;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

/**
 * Author by yanxiaobo
 * Date on 2018/4/16$.
 */
@Getter
@Setter
@Builder
public class PermissionVO {
    private Integer menuId;
    private Integer parentId;
    private String parentName;
    private String identifier;//用于标识权限
    private String description;
    private String name;
    private String url;//对应权限的menuurl（页面url）
    private String perms;//该权限对应拦截的url
    private int type;
    private int orderNum;//对应权限的order字段
    private String icon;

    public static PermissionVO adapter(Permission permission) {
        PermissionVO permissionVO = PermissionVO.builder()
                .menuId(permission.getId())
                .name(permission.getPermissionName())
                .orderNum(permission.getOrder())
                .type(permission.getType())
                .url(permission.getMenuURL())
                .perms(permission.getUrl())
                .description(permission.getPermissionDescription())
                .icon(permission.getIcon())
                .identifier(permission.getIdentifier())
                .build();
        if(null != permission.getParent()) {
            permissionVO.setParentId(permission.getParent().getId());
            permissionVO.setParentName(permission.getParent().getPermissionName());
        }
        return permissionVO;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == this) return true;
        if (!(obj instanceof PermissionVO)) {
            return false;
        }
        PermissionVO permissionVO = (PermissionVO) obj;
        return permissionVO.getMenuId() == menuId;
    }

    @Override
    public int hashCode() {
        return name.hashCode();
    }
}
